package com.chtw.oasystem.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author chtw
 * @date 2020-11-16 22:29
 */
public class TimeUtils {

    public final static String FULL_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public final static String DAY_TIME_FORMAT = "yyyy-MM-dd";

    public static int currentTimeMillis() {
        return (int)(new Date().getTime() / 1000);
    }

    public static String formatDate(Date date, String format) {
        if (format == null) {
            format = FULL_TIME_FORMAT;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    public static String getToday() {
        Date date = new Date();
        return formatDate(date, DAY_TIME_FORMAT);
    }

    public static int getTimestamp(String format, String date) {
        if (format == null) {
            format = DAY_TIME_FORMAT;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            return (int)(sdf.parse(date).getTime() / 1000);
        } catch (Exception e) {
            return 0;
        }
    }


    public static void main(String[] args) {
        int now = TimeUtils.currentTimeMillis();
        String today = TimeUtils.getToday();
        int h = TimeUtils.getTimestamp(TimeUtils.FULL_TIME_FORMAT, today + " 09:00:00");
        System.out.println(now);
        System.out.println(h);
        if (h > now) {
            System.out.println(122323);
        }

    }
}
